import axios from '../../utils/axios'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    orderInfo: null,
    showList: [],
    attach: [],
    payType: {},
    showShoppCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("orderDetail的参数：", options)
    this.setData({
      orderId: options.orderId
    })
    this.orderDetail()
  },
  // 根据orderId查询订单详情
  orderDetail() {
    let that = this

    axios('/groupon/goodsOrderDetail', {
      order_id: that.data.orderId
    }).then(res => {
      if (res.data.resultcode == '1') {
        that.setData({
          attach: res.data.result.server.attach,
          orderInfo: res.data.result,
          payType: res.data.result.status
        });
        that.refreshShoppList(that.data.orderInfo.server.list)
      }
    })

  },
  // 联系店铺
  phoneCall: function (e) {
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
      success: function () { },
    })
  },

  // 申请退款
  refundOrder: function (e) {
    let that = this
    wx.showModal({
      content: '确认申请退款吗？',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          axios('/personal/refundGoods', {
            order_id: that.data.orderId
          }).then(res => {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          })
        }
      }
    })
  },

  // 取消订单
  cancelOrder() {
    let that = this
    wx.showModal({
      content: '确定取消该订单吗？',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          axios('/groupon/cancelGoodsOrder', {
            order_id: that.data.orderId
          }).then(res => {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 刷新购物车list
  refreshShoppList(shoppList) {

    let leng = shoppList.length,
      that = this,
      tempList = [],
      showList = [],
      repetitionList = [];

    // 找到数组中重复的名字，（不能根据serverId来判断，因为一个同样的商品、serverId一样、但是规格不同）
    for (let i = 0; i < leng; i++) {
      if (tempList.indexOf(shoppList[i].name) == -1) {
        tempList.push(shoppList[i].name)
        showList.push(shoppList[i])
      } else {
        repetitionList.push(shoppList[i])
      }
    }

    // showList是去重后的numArray，repetitionList是选择重复的商品数组
    for (let i = 0, leng1 = showList.length; i < leng1; i++) {
      // 拿当前showList的每一个跟重复值对比，如果存在，那么就新增一个属性count，每存在一个count就++
      let index = 1 // 值默认为1，因为它本身也是一个商品
      for (let j = 0, leng2 = repetitionList.length; j < leng2; j++) {
        if (showList[i].name == repetitionList[j].name) {
          showList[i].attach_name = "";
          // 进来这里说明这个商品被选择了多次，那么count就是记录一共被选择了多少次
          showList[i].shoppCount = ++index

          showList[i].price = parseFloat(showList[i].price)
          repetitionList[j].price = parseFloat(repetitionList[j].price)
          showList[i].discount = parseFloat(showList[i].discount)
          repetitionList[j].discount = parseFloat(repetitionList[j].discount)

          showList[i].price += repetitionList[j].price
          showList[i].discount += repetitionList[j].discount
        }
      }
    }

    that.setData({
      showList: showList,
      showShoppCount: leng
    })

    that.refreshAttachList()
  },
  //刷新购物车规格
  refreshAttachList() {
    const self = this;
    let i, j, o, goodsId = [],
      obj = [],
      attachList = this.data.attach,
      list = this.data.showList,
      listLen = this.data.showList.length,
      len = attachList.length;
    for (i = 0; i < len; i++) {
      if (goodsId.indexOf(attachList[i].goods_id) == -1) {
        goodsId.push(attachList[i].goods_id);
        obj.push(attachList[i])
      }
    }

    //根据showlist对应的id追加到showlist里
    for (j = 0; j < listLen; j++) {
      for (o = 0; o < obj.length; o++) {
        if (list[j].goods_id == obj[o].goods_id) {
          let attach_name = 'showList[' + j + '].attach_name';
          self.setData({
            [attach_name]: obj[o].name
          })
        }
      }
    }

  }
})